package com.maimao.order.aop;

import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransaction;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author MaoLin Wang
 * @date 2021/4/5 3:31 下午
 */
@Slf4j
@Component
//@Aspect
@Deprecated
public class GlobalATTractionAspect {

    @Pointcut("@annotation(io.seata.spring.annotation.GlobalTransactional)")
    public void txAnnotation(){

    }

    @AfterThrowing(throwing = "throwable", pointcut = "txAnnotation()")
    public void doAfterReturning(Throwable throwable) {
        GlobalTransaction globalTransaction =
                GlobalTransactionContext.getCurrent();
        if (globalTransaction == null) {
            log.info("globalTransaction is null,not rollback------》", globalTransaction.getXid());

            return;
        }

        log.info("AOP------- 全局事务回滚-----xid:{}------》", globalTransaction.getXid());
        try {
            globalTransaction.rollback();
        } catch (TransactionException e) {
            e.printStackTrace();
            log.info("AOP------- 全局事务回滚失败-----xid:{}------》", globalTransaction.getXid());
        }
    }
}
